﻿//using MySql.Data.MySqlClient;
//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.Data.SqlClient;
//using System.Linq;
//using System.Web;
//using ZHDJ.Core.Util;
//using ZHDJ.Data;

//namespace ZHDJ.MobileInterface.Common
//{
//    public class ATMMassageCS
//    {
//        public static bool ATMSaveAndTakeIsExists(string saveOrTakeID)
//        {
//            using (SqlCommand cmd = new SqlCommand())
//            {
//                cmd.CommandText = string.Format("select count(*) from tblPartyATMSaveAndTake where TakeID = '{0}' or SaveID = '{0}'", saveOrTakeID);

//                object obj = ZHDJ.Data.MySqlHelper.ExecuteScalar(cmd.CommandText);
//                if (obj == null)
//                { return false; }
//                else
//                { return Convert.ToInt32(obj) > 0; }
//            }
//        }

//        /// <summary>
//        /// 尚未匹对
//        /// </summary>
//        /// <param name="page_no"></param>
//        /// <param name="page_count"></param>
//        /// <param name="saveOrTake"></param> 
//        /// <returns></returns>
//        public static DataSet QueryNoPageList(int page_no, int page_count, string saveOrTake)
//        {  
//            string field = string.Empty;
//            string table = string.Empty;
//            string where = " 1=1 ";
//            string orderBy = string.Empty;
//            string url = string.Empty;
             
//            field = "*";
//            table = @"(select a.ID,a.Name as Title, a.CreateTime,a.EndTime, 'Save' as SavaOrTake, a.BranchID,b.BranchName
//	                        from tblPartyATMSaveThings a 
//	                        left join tblPartyOrgBranch b on a.BranchID=b.BranchID
//	                        where a.ID not in (select SaveID from tblPartyATMSaveAndTake) and a.EndTime >= '2013-05-05' 
//                        union 
//                        select a.ID,a.Name as Title, a.CreateTime,a.EndTime, 'Take' as SavaOrTake, a.BranchID,b.BranchName
//	                        from tblPartyATMTakeThings a 
//	                        left join tblPartyOrgBranch b on a.BranchID=b.BranchID
//	                        where a.ID not in (select TakeID from tblPartyATMSaveAndTake) and a.EndTime >= '2013-05-05') aaa";
             
//            if (saveOrTake == "1")
//            { where += @" and SavaOrTake='Save'  "; }
//            else if (saveOrTake == "2")
//            { where += @" and SavaOrTake='Take'  "; }
//            else if (saveOrTake == "0")
//            { ; }
//            else
//            { where += @" and 1=2  "; }

//            orderBy = "CreateTime DESC ";

//            return CommonUtils.QueryByPage(field, table, where, orderBy, page_no, page_count);
//        }

//        /// <summary>
//        /// 已匹对
//        /// </summary>
//        /// <param name="page_no"></param>
//        /// <param name="page_count"></param>
//        /// <param name="saveOrTake"></param> 
//        /// <returns></returns>
//        public static DataSet QueryPageList(int page_no, int page_count, string saveOrTake)
//        {
//            string field = string.Empty;
//            string table = string.Empty;
//            string where = " 1=1 ";
//            string orderBy = string.Empty;
//            string url = string.Empty;

//            field = "*";
//            table = @"(select a.ID as SaveID, a.Name as Title ,
//                        (select top 1 c.ID from tblPartyATMSaveAndTake b left join tblPartyATMTakeThings c on b.TakeID = c.ID where b.SaveID = a.ID and b.Status = '2') as TakeID ,
//                        (select top 1 c.Name from tblPartyATMSaveAndTake b left join tblPartyATMTakeThings c on b.TakeID = c.ID where b.SaveID = a.ID and b.Status = '2') as MatchTitle ,
//                        (select top 1 b.CreateTime from tblPartyATMSaveAndTake b left join tblPartyATMTakeThings c on b.TakeID = c.ID where b.SaveID = a.ID and b.Status = '2') as MatchTime ,
//                        'Save' as SavaOrTake,
//                        a.BranchID
//                        from tblPartyATMSaveThings a 
//                        where a.ID in (select SaveID from tblPartyATMSaveAndTake) and a.Status = '2'
//                        union
//                        select (select top 1 c.ID from tblPartyATMSaveAndTake b left join tblPartyATMSaveThings c on b.SaveID = c.ID where b.TakeID = a.ID and b.Status = '2') as SaveID ,a.Name as Title ,
//                        a.ID as TakeID,
//                        (select top 1 c.Name from tblPartyATMSaveAndTake b left join tblPartyATMSaveThings c on b.SaveID = c.ID where b.TakeID = a.ID and b.Status = '2') as MatchTitle ,
//                        (select top 1 b.CreateTime from tblPartyATMSaveAndTake b left join tblPartyATMSaveThings c on b.SaveID = c.ID where b.TakeID = a.ID and b.Status = '2') as MatchTime ,
//                        'Take' as SavaOrTake,
//                        a.BranchID
//                        from tblPartyATMTakeThings a 
//                        where a.ID in (select TakeID from tblPartyATMSaveAndTake) and a.Status = '2') aaa";
             
//            if (saveOrTake == "1")
//            { where += @" and SavaOrTake='Save'  "; }
//            else if (saveOrTake == "2")
//            { where += @" and SavaOrTake='Take'  "; }
//            else if (saveOrTake == "0")
//            { ; }
//            else
//            { where += @" and 1=2  "; }

//            orderBy = "MatchTime DESC ";

//            return CommonUtils.QueryByPage(field, table, where, orderBy, page_no, page_count);
//        }


//        public static DataTable QuerySaveInfo(string id)
//        {
//            using (SqlCommand cmd = new SqlCommand())
//            {
//                cmd.CommandText = @"SELECT Data.*,
//ST.ParaMemberName as TitularName,STC.ParaMemberName as ThingTypeName,
//(CASE data.IsEndTime WHEN 1 THEN '是' ELSE '否' END) as IsEndTimeName,
//(CASE data.Purposes WHEN 2 THEN Data.PurposesDescription ELSE SP.ParaMemberName END) as PurposesName,
//SSM.ParaMemberName as SaveModusName,
//OM.UserName as CreateByName,OB.BranchName as CreateByBranchName,
//(CASE data.IsSub WHEN 1 THEN '是' ELSE '否' END) as IsSubName,
//SS.ParaMemberName as StatusName 
//FROM tblPartyATMSaveThings Data
//left join tblPartyOrgMembers OM on OM.LoginID = Data.CreateBy
//left join tblPartyOrgBranch OB on OB.BranchID = Data.BranchID and OB.BranchID =OM.BranchID
//left join tblSysSystemParameter ST on ST.ParaMember = Data.Titular and ST.ParaType = 'SXT.ATM.Titular'
//left join tblSysSystemParameter STC on STC.ParaMember = Data.ThingType and STC.ParaType = 'SXT.ATM.ThingType'
//left join tblSysSystemParameter SP on SP.ParaMember = Data.Purposes and SP.ParaType = 'SXT.ATM.Purposes'
//left join tblSysSystemParameter SSM on SSM.ParaMember = Data.SaveModus and SSM.ParaType = 'SXT.ATM.SaveModus'
//left join tblSysSystemParameter SS on SS.ParaMember = Data.Status and SS.ParaType = 'SXT.ATM.SaveStatus'
//                                    WHERE data.ID = ?ID;";
                  
//                MySqlParameter[] parameters = new MySqlParameter[]
//                {
//                new MySqlParameter("?ID",MySqlDbType.VarChar)
//                };
//                parameters[0].Value = id;

//                return ZHDJ.Data.MySqlHelper.ExecuteDataTable(cmd.CommandText, parameters);
//            }
//        }

//        public static DataTable QueryTakeInfo(string id)
//        {
//            using (SqlCommand cmd = new SqlCommand())
//            {
//                cmd.CommandText = @"select Data.*,
//ST.ParaMemberName as TitularName,STC.ParaMemberName as ThingTypeName,
//(CASE data.IsEndTime WHEN 1 THEN '是' ELSE '否' END) as IsEndTimeName,
//(CASE data.Purposes WHEN 2 THEN Data.PurposesDescription ELSE SP.ParaMemberName END) as PurposesName,
//SSM.ParaMemberName as SaveModusName,
//OM.UserName as CreateByName,OB.BranchName as CreateByBranchName,
//(CASE data.IsSub WHEN 1 THEN '是' ELSE '否' END) as IsSubName,
//SS.ParaMemberName as StatusName 
//FROM tblPartyATMTakeThings Data
//left join tblPartyOrgMembers OM on OM.LoginID = Data.CreateBy
//left join tblPartyOrgBranch OB on OB.BranchID = Data.BranchID and OB.BranchID =OM.BranchID
//left join tblSysSystemParameter ST on ST.ParaMember = Data.Titular and ST.ParaType = 'SXT.ATM.Titular'
//left join tblSysSystemParameter STC on STC.ParaMember = Data.ThingType and STC.ParaType = 'SXT.ATM.ThingType'
//left join tblSysSystemParameter SP on SP.ParaMember = Data.Purposes and SP.ParaType = 'SXT.ATM.Purposes'
//left join tblSysSystemParameter SSM on SSM.ParaMember = Data.TakeModus and SSM.ParaType = 'SXT.ATM.SaveModus'
//left join tblSysSystemParameter SS on SS.ParaMember = Data.Status and SS.ParaType = 'SXT.ATM.TakeStatus'
//                                    WHERE data.ID = ?ID;";
                
//                MySqlParameter[] parameters = new MySqlParameter[]
//                { 
//                new MySqlParameter("?ID",MySqlDbType.VarChar)
//                };
//                parameters[0].Value = id; 

//                return ZHDJ.Data.MySqlHelper.ExecuteDataTable(cmd.CommandText, parameters);
//            }
//        }

//        public static bool ATMWantMatchTake(ATMWantMatchTakeInParam param, string saveName, string thingType, string brandID)
//        {
//            using (SqlCommand cmd = new SqlCommand())
//            {
//                string takeID = StringHelper.NewID;
//                string sql = string.Format(@" update tblPartyATMSaveThings set Status = '1' where id = '{0}';

//                                                insert into tblPartyATMTakeThings(ID,CreatebyAddress,CreatebyContact,Description,Name,
//                                                PurposesDescription,ThingCount,Purposes,TakeModus,Status,
//                                                ThingType,Titular,IsEndTime,EndTime,IsLoveActivity,
//                                                SignX,SignY,Createby,BranchID,CreateTime) 
//                                                values
//                                                ('{1}','{2}','{3}','{4}','{5}',
//                                                '','{6}','','','1',
//                                                '{7}','','1','{8}','0',
//                                                '','','{9}','{10}','{11}');", 
//                              param.SaveID, takeID, param.CreatebyAddress, param.CreatebyContact, param.Description,saveName,
//                              param.ThingCount, thingType, param.EndTime, param.UserID, brandID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//                cmd.CommandText = sql;
                 
//                bool IsOK = ZHDJ.Data.MySqlHelper.ExecuteNonQuery(cmd.CommandText) > 0;

//                PartyATMSaveAndTake oModelSA = new PartyATMSaveAndTake();
//                oModelSA.ID = StringHelper.NewID;
//                oModelSA.TakeID = takeID;
//                oModelSA.SaveID = param.SaveID;
//                oModelSA.Status = "1";
//                oModelSA.Createby = param.UserID;
//                oModelSA.BranchID = brandID;
//                oModelSA.CreateTime = DateTime.Now;
//                oModelSA.Description = "";
//                oModelSA.IsSub = false;
//                oModelSA.IsSync = "0";
//                oModelSA.AdminAudit = "0";
//                oModelSA.UserAudit = "2";
//                AddATMSaveAndTake(oModelSA);

//                InterfaceHelper.AddTask(oModelSA.ID, "爱心活动", "爱心柜员机", param.UserID,
//                new string[] { oModelSA.BranchID }, "../../../PersonalCenter/Module/ATM/ATMMatchManagerAudit.aspx?RelationID=" + oModelSA.ID + "&LoginID="
//                        + param.UserID + "&BranchID=" + brandID, "Branch");

//                return true;
//            } 
//        }

//        public static bool ATMWantMatchSave(ATMWantMatchSaveInParam param, string saveName, string thingType,string takeModus, string brandID)
//        {
//            using (SqlCommand cmd = new SqlCommand())
//            {
//                //string saveID = StringHelper.NewID;
//                //string sql = string.Format(@" update tblPartyATMTakeThings set Status = '1' where ID='{0}';

//                //                                insert into tblPartyATMSaveThings(ID,CreatebyAddress,CreatebyContact,Description,Name,
//                //                                Purposes,PurposesDescription,ThingCount,SaveModus,Status,
//                //                                ThingType,Titular,IsEndTime,EndTime,SignX,
//                //                                SignY,Createby,BranchID,CreateTime)
//                //                                values
//                //                                ('{1}','{2}','{3}','{4}','{5}',
//                //                                '{6}','{7}','{8}','{9}','1',
//                //                                '{10}','0','1','{11}','',
//                //                                '','{12}','{13}','{14}');",
//                //              param.TakeID, saveID, param.CreatebyAddress, param.CreatebyContact, param.Description, saveName,
//                //              param.Purposes, param.PurposesDescription, param.ThingCount, takeModus, 
//                //              thingType, param.EndTime, 
//                //              param.UserID, brandID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//                //cmd.CommandText = sql;
//                //bool IsOK = DB.DBManager.DBSettings["SXT.DefaultDBEx"].ExecuteNonQuery(cmd) > 0;

//                //PartyATMSaveAndTake oModelSA = new PartyATMSaveAndTake();
//                //oModelSA.ID = StringHelper.NewID;
//                //oModelSA.TakeID = param.TakeID;
//                //oModelSA.SaveID = saveID;
//                //oModelSA.Status = "1";
//                //oModelSA.Createby = param.UserID;
//                //oModelSA.BranchID = brandID;
//                //oModelSA.CreateTime = DateTime.Now;
//                //oModelSA.Description = "";
//                //oModelSA.IsSub = false;
//                //oModelSA.IsSync = "0";
//                //oModelSA.AdminAudit = "0";
//                //oModelSA.UserAudit = "2";
//                //AddATMSaveAndTake(oModelSA);
                 
//                //SXT.Party.Utility.PartyTask.AddTask(oModelSA.ID, "爱心活动", "爱心柜员机", param.UserID,
//                //new string[] { oModelSA.BranchID }, "../../../PersonalCenter/Module/ATM/ATMMatchManagerAudit.aspx?RelationID=" + oModelSA.ID + "&LoginID="
//                //+ param.UserID + "&BranchID=" + brandID, "Branch");

//                return true;
//            }
//        }

//        public static bool AddATMSaveAndTake(PartyATMSaveAndTake model)
//        {
//            return true;
////            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
////            {
////                string cmdSearch = @"delete from tblPartyATMSaveAndTake where SaveID = @SaveID and TakeID = @TakeID;
////insert into tblPartyATMSaveAndTake(ID, SaveID, TakeID, Status, IsSub, Description, Createby, BranchID, CreateTime,IsSync,AdminAudit,UserAudit)
////values(@ID, @SaveID, @TakeID, @Status, @IsSub, @Description, @Createby, @BranchID, @CreateTime,@IsSync,@AdminAudit,@UserAudit);
////update tblPartyATMSaveThings set Status = @Status where ID = @SaveID;
////update tblPartyATMTakeThings set Status = @Status where ID = @TakeID;";
////                cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = string.Format("{0}", model.ID);
////                cmd.Parameters.Add("@SaveID", SqlDbType.VarChar).Value = string.Format("{0}", model.SaveID);
////                cmd.Parameters.Add("@TakeID", SqlDbType.VarChar).Value = string.Format("{0}", model.TakeID);
////                cmd.Parameters.Add("@Status", SqlDbType.Int).Value = string.Format("{0}", model.Status);
////                cmd.Parameters.Add("@IsSub", SqlDbType.Bit).Value = string.Format("{0}", model.IsSub);
////                cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = string.Format("{0}", model.Description);
////                cmd.Parameters.Add("@Createby", SqlDbType.VarChar).Value = string.Format("{0}", model.Createby);
////                cmd.Parameters.Add("@BranchID", SqlDbType.VarChar).Value = string.Format("{0}", model.BranchID);
////                cmd.Parameters.Add("@CreateTime", SqlDbType.DateTime).Value = string.Format("{0}", model.CreateTime);
////                cmd.Parameters.Add("@IsSync", SqlDbType.Int).Value = string.Format("{0}", model.IsSync);
////                cmd.Parameters.Add("@AdminAudit", SqlDbType.Int).Value = string.Format("{0}", model.AdminAudit);
////                cmd.Parameters.Add("@UserAudit", SqlDbType.Int).Value = string.Format("{0}", model.UserAudit);
////                cmd.CommandText = cmdSearch;
////                return SXT.DB.DBManager.DBSettings["SXT.DefaultDBEx"].ExecuteNonQuery(cmd) >= 0;
////            }
//        } 
//    }
//}